<template>
  <div class="songs">
    <div class="songs-nav">
      <ul class="songs-top-nav">
        <li
          class="songs-top-nav-item"
          :class="[activeIndex === index ? 'active' : '']"
          @click="handleClicknav(index)"
          v-for="(nav, index) of navs"
          :key="nav.name"
        >
          {{ nav.name }}
        </li>
      </ul>
    </div>
    <div class="songs-senter">
      <ul class="songs-senter-list">
        <li
          class="songs-senter-item"
          @click="PlayMusic(item, SongsList, index)"
          v-for="(item, index) of SongsList"
          :key="item.id"
        >
          <div
            class="songs-senter-box"
            :class="[songsListColor === index ? 'current' : '']"
          >
            <span class="songs-senter-number">{{
              index + 1 >= 10 ? index + 1 : "0" + (index + 1)
            }}</span>
            <div class="songs-senter-img">
              <img
                class="songs-img-item"
                :src="$utils.getImgUrl(item.album.picUrl, 120, 120)"
              />
              <div class="songs-senter-img-btn">
                <i class="play-icon iconfont icon-component icon-play"></i>
              </div>
            </div>
            <span class="songs-senter-songs">{{ item.name }}</span>

            <span class="songs-senter-singer">{{ getName(item.artists) }}</span>
            <span class="songs-senter-songs-right">{{ item.name }}</span>
            <span class="songs-senter-time">{{
              getsongstime(item.bMusic.playTime)
            }}</span>
          </div>
        </li>
      </ul>
    </div>
    <div class="songs-foot"></div>
  </div>
</template>

<script>
//https://api.mtnhao.com/top/song?type=0
import { fetchNewSong } from "../api";
//引入vuex的方法
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      navs: [
        { name: "全部", type: 0 },
        { name: "华语", type: 7 },
        { name: "欧美", type: 96 },
        { name: "日本", type: 8 },
        { name: "韩国", type: 16 },
      ],
      activeIndex: 0,
      // activeNva: "全部",

      page: 1,
      pageSize: 50,
      total: 0,
      SongsList: [],
      type: 0,
    };
  },
  computed: {
    ...mapState(["songsListColor", "isPlay"]),
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getSongslist();
    },
    //mapmutatinns//放在方法里面
    ...mapMutations([
      "setCurrentSong",
      "setSongsList",
      "setSongsListColor",
      "setplaystatus",
      "setRecordPlaylist",
    ]),

    handleClicknav(index) {
      this.activeIndex = index;
      (this.type = this.navs[index].type),
        //
        this.getSongslist();
    },
    Changes(page) {
      this.page = page;
      this.getSongslist();
    },
    getName(arr = []) {
      return arr
        .map((item) => {
          return item.name;
        })
        .join("/");
    },
    getSongslist() {
      fetchNewSong({
        offset: (this.page - 1) * this.pageSize,
        limit: this.pageSize,
        type: this.type,
      }).then((res) => {
        this.SongsList = res.data;
        this.total = res.total;
        //console.log(this.SongsList);
      });
    },
    //点击发送数据过去
    PlayMusic(song, songs, index) {
      //this.$eventBus.$emit("PlayMusic", song, arr);
      //console.log(song);
      this.setCurrentSong(song);
      this.setSongsList(songs);
      this.setSongsListColor(index);
      this.setRecordPlaylist(song);
      this.setplaystatus(true);
      // this.setplaystatus(this.isPlay);
    },

    getsongstime(item) {
      const ST = item / 1000 / 60;
      const M = parseInt(ST);
      const S = (ST - M) * 60;
      const SS = Math.ceil(S);
      // const S = ST % 60;
      const SSS = String(SS).length === 1 ? "0" + SS : SS;

      const Times = M + ":" + SSS;

      return Times;
    },
  },
};
</script>

<style lang='less' scoped>
.songs {
  padding: 12px;
  .songs-nav {
    height: 40px;
    .songs-top-nav {
      height: 100%;
      flex: 1;
      float: right;
      margin-top: 15px;
      overflow: hidden;
      //触发bfc
    }
    .songs-top-nav-item {
      height: 100%;
      float: left;
      font-size: 12px;
      color: #666;
      margin-right: 20px;

      cursor: pointer; //鼠标

      &:hover {
        color: black;
      }
      &.active {
        color: red;
      }
    }
  }
  .songs-senter {
    // overflow: hidden;
    height: 100%;
    // display: flex;
    .songs-senter-list {
      width: 100%;
      // display: flex;
      // flex-wrap: wrap;
      padding-top: 8px;
    }
    .songs-senter-item {
      min-width: 120px;
      position: relative;
      // padding: 0 16px 18px 0px;
      flex-shrink: 1;
      flex: 1;
      cursor: pointer;
      border-radius: 5px;
      &:hover {
        background-color: rgb(231, 231, 231);
      }
    }
    .songs-senter-box {
      position: relative;
      height: 67px;
      left: 5px;
      font-size: 12px;

      color: #606266;
      &.current {
        color: red;
      }

      .songs-senter-number {
        position: absolute;
        left: 10px;
        line-height: 72px;

        text-align: center;
        color: #606266;
      }
      .songs-senter-img {
        position: absolute;
        top: 4px;
        left: 90px;
        .songs-img-item {
          height: 60px;
          width: 60px;
          border-radius: 7px;
        }
        .songs-senter-img-btn {
          position: absolute;
          bottom: 18px;
          right: 14px;
          width: 30px;
          height: 30px;
          background-color: rgba(255, 255, 255, 0.6);
          line-height: 36px;
          text-align: center;
          color: red;
          border-radius: 50%;
          .icon-play {
            position: absolute;
            font-size: 15px;
            margin-left: -6px;
            margin-top: -2px;
          }
        }
      }
      .songs-senter-songs,
      .songs-senter-singer,
      .songs-senter-songs-right,
      .songs-senter-time {
        width: 180px;
        line-height: 72px;
        //文本三件套
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .songs-senter-songs {
        position: absolute;
        left: 230px;

        // white-space: nowrap;
        // overflow: hidden;
        // text-overflow: ellipsis;
      }
      .songs-senter-singer {
        position: absolute;
        left: 440px;
      }

      .songs-senter-songs-right {
        position: absolute;
        left: 640px;
      }
      .songs-senter-time {
        position: absolute;
        left: 820px;
      }
    }
  }
}
</style>